package com.example.order.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.common.Dto;
import com.example.common.DtoUtil;
import com.example.mapper.GdOrderMapper;
import com.example.pojo.GdOrder;
import com.example.service.RpcOrderService;
import com.example.utils.*;
import com.example.vo.OrderMessageVo;
import org.apache.solr.common.util.DateUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

/**
 * Created by LENOVO on 2018/9/23.
 */
@Component
@Service(interfaceClass=RpcOrderService.class)
@EnableTransactionManagement
@PropertySource({"classpath:application.properties"})
public class RpcOrderServiceImpl implements RpcOrderService {
    @Resource
    private GdOrderMapper qgOrderMapper;

    @Resource
    private RedisUtils redisUtils;

    @Override
    public Dto createOrder(OrderMessageVo orderMessageVo) {
        GdOrder gdOrder = new GdOrder();
        try {
            gdOrder.setUserid(Long.parseLong(orderMessageVo.getUserId()));
            gdOrder.setPayamount(BigDecimal.valueOf(orderMessageVo.getAmount()));
            gdOrder.setDoctorid(orderMessageVo.getDoctorId());
            gdOrder.setCreationdate(new Date());
            gdOrder.setDoctorname(orderMessageVo.getDoctorName());
            gdOrder.setOrderno(createOrderNo(gdOrder));
            gdOrder.setOrderstatus(Constants.OrderStatus.TOPAY);
            gdOrder.setPaytype(orderMessageVo.getPaytype());
            gdOrder.setServiceid(orderMessageVo.getServiceId());
            gdOrder.setTradeno(orderMessageVo.getStatus());
            qgOrderMapper.insertSelective(gdOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常", "0002");
        }
        return DtoUtil.returnSuccess();
    }

    @Override
    public GdOrder loadOrderByOrderNo(String orderNo) throws Exception {
        List<GdOrder> orders = qgOrderMapper.getOrderByOrderNo(orderNo);
        if (orders.size() == 1) {
            return orders.get(0);
        } else
        {
            return null;
        }
    }

    private String createOrderNo(GdOrder gdOrder) {
        StringBuilder md5String = new StringBuilder();
        md5String.append(gdOrder.getDoctorid());
        md5String.append(gdOrder.getUserid());
        md5String.append(System.currentTimeMillis());
        md5String.append(Math.random() * 1000000);
        String md5 = MD5.getMd5(md5String.toString(), 6);
        //生成订单编号
        StringBuilder orderNo = new StringBuilder();
        orderNo.append(md5);
        Random random=new Random();
        String val="";
        for(int i=0;i<6;i++){
            String str = random.nextInt( 2 ) % 2 == 0 ? "num" : "char";
            if ( "char".equalsIgnoreCase( str ) )
            {
                int nextInt = random.nextInt( 2 ) % 2 == 0 ? 65 : 97;
                val += (char) ( nextInt + random.nextInt( 26 ) );
            }
            else if ( "num".equalsIgnoreCase( str ) )
            {
                val += String.valueOf( random.nextInt( 10 ) );
            }
            orderNo.append(val);
        }
        return orderNo.toString();
    }

    @Override
    public boolean processed(String orderNo) throws Exception {
        GdOrder gdOrder = this.loadOrderByOrderNo(orderNo);
        return gdOrder.getOrderstatus().equals(2);
    }

    @Override
    public void updateOrder(String orderNo, String tradeNo) throws Exception {

        GdOrder gdOrder = this.loadOrderByOrderNo(orderNo);
        gdOrder.setOrderstatus(Constants.OrderStatus.SUCCESS);
        gdOrder.setTradeno(tradeNo);
        qgOrderMapper.updateByStatus(gdOrder.getOrderno());
    }

    @Override
    public GdOrder getOrderById(String id) throws Exception {
        return qgOrderMapper.selectByPrimaryKey(Long.parseLong(id));
    }

    @Override
    public List<GdOrder> getOrderListByMap(Map<String, Object> param) throws Exception {
        return qgOrderMapper.getQgOrderListByMap(param);
    }

    @Override
    public boolean flushOrderStatus() throws Exception {
        Integer flag;
        flag = qgOrderMapper.flushCancelOrderStatus();
        return flag > 0 ? true : false;
    }
}
